<?php

class EntryController extends Zend_Controller_Action {

    public function indexAction() {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        
        if ($_SERVER['REQUEST_METHOD'] == 'GET') {
            $this->handleRealtimeVerification();
        } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $this->handleRealtimeData();
        }
    }

    protected function handleRealtimeVerification() {
        $verify_token = 'mkt_studio';
        if (isset($_GET['hub_mode']) &&
                $_GET['hub_mode'] == 'subscribe' &&
                isset($_GET['hub_verify_token']) &&
                $_GET['hub_verify_token'] == $verify_token) {
            echo $_GET['hub_challenge'];
        }
    }

    protected function handleRealtimeData() {
        $post_body   = file_get_contents('php://input');
        $json_string = preg_replace('/([^\\\])":([0-9]{10,})(,|})/', '$1":"$2"$3', $post_body);
        $obj = json_decode($json_string, true);
        $entries = $obj['entry'];
        
        if (empty($entries)) {
            return;
        }

        $result = array();
        foreach ($entries as $entry) {
            $created_time = $entry['time'];
            $id_social_page = $entry['id'];
            $changes = $entry['changes'];

            foreach ($changes as $change) {
                $value = $change['value'];
                $item = $value['item'];
                $verb = $value['verb'];

                if ($verb == 'remove') {
                    continue;
                }

                $data = array(
                    'created_time' => $created_time,
                    'id_social_page' => $id_social_page
                );

                switch ($item) {
                    case 'like':
                        if (!isset($value['user_id'])) { // not like page event
                            $data['id_social_user'] = $value['sender_id'];
                            $data['id_social_post'] = $value['parent_id'];

                            $this->handleRealtimeLikeData($data);
                        } else {
                            //TODO: handle like page action here
                        }
                        break;
                    case 'comment':
                        $data['id_social_user'] = $value['sender_id'];
                        $data['id_social_post'] = $value['parent_id'];
                        $data['id_social_comment'] = $value['comment_id'];
                        $data['created_time'] = $value['created_time'];

                        $this->handleRealtimeCommentData($data);
                        break;
                    case 'status':
                        $data['id_social_post'] = $value['post_id'];

                        $this->handleRealtimePostData($data);
                        break;
                    case 'post':
                        $data['id_social_post'] = $id_social_page . "_{$value['post_id']}";

                        $this->handleRealtimePostData($data);
                        break;
                }
            }
        }
        return $result;
    }

    protected function handleRealtimeLikeData($realtime_data) {
        if ($realtime_data['id_social_user'] != $realtime_data['id_social_page']) {
            $likes_db = new Core_Model_DbTable_BufferLikes();
            $likes_db->insertCustom($realtime_data);
        }
    }

    protected function handleRealtimeCommentData($realtime_data) {
        if ($realtime_data['id_social_user'] != $realtime_data['id_social_page']) {
            $realtime_comment_db = new Core_Model_DbTable_BufferComments();
            $realtime_comment_db->insertCustom($realtime_data);
        }
    }

    protected function handleRealtimePostData($realtime_data) {
        $realtime_post_db = new Core_Model_DbTable_BufferPosts();
        $realtime_post_db->insertCustom($realtime_data);
    }
}
